package com.xiaoze.springcloud.service;

import com.alibaba.csp.sentinel.annotation.SentinelResource;
import com.alibaba.csp.sentinel.slots.block.BlockException;
import com.xiaoze.springcloud.dao.UserDao;
import com.xiaoze.springcloud.entity.User;
import com.xiaoze.springcloud.service.UserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * UserServiceImpl
 *
 * @author xiaoze
 * @date 2018/6/3
 *
 */
@Slf4j
@Service
public class UserServiceImpl implements UserService {

    @Autowired
    private UserDao userDao;

    @Override
    @SentinelResource(value = "get", blockHandler = "exceptionHandler", fallback = "userFallback")
    public User get(String userNo) {
        User user;
        user=userDao.get(userNo);
        return user;

    }

    /**
     * Fallback 函数，函数签名与原函数一致或加一个 Throwable 类型的参数.
     * @param userNo 用户账号
     * @param throwable 父类异常
     * @return User
     */
    public User userFallback(String userNo, Throwable throwable) {
        log.error("fallbackHandler：" + userNo);
        User user = new User();
        user.setUserName("错误账户");
        user.setUserPwd("错误密码");
        return user;
    }

    /**
     * Block 异常处理函数，参数最后多一个 BlockException，其余与原函数一致.
     * @param userNo 用户账号
     * @param blockException block异常
     * @return User
     */
    public User exceptionHandler(String userNo, BlockException blockException) {
        log.error("blockHandler：" + userNo);
        User user = new User();
        user.setUserName("Sentinel错误账户");
        user.setUserPwd("Sentinel错误密码");
        return user;
    }


}